package org.easydarwin.adapter;

import android.content.Context;
import android.media.Image;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import org.easydarwin.beans.Role;
import org.easydarwin.beans.User;
import org.easydarwin.easyrtc.R;
import org.easydarwin.listener.OnGroupExpandedListener;

import java.util.List;

/**
 * @author Richie on 2017.07.31
 *         改过 Indicator 的 ExpandableListView 的适配器
 */
public class IndicatorExpandableListAdapter extends BaseExpandableListAdapter {
    private static final String TAG = "IndicatorExpandableList";
    private List<Role> roles;
    //                用于存放Indicator的集合
    private SparseArray<ImageView> mIndicators;
    private OnGroupExpandedListener mOnGroupExpandedListener;
    private Context mContent;
    private PhoneClickListener listener;

    public IndicatorExpandableListAdapter(Context  context, List<Role> roles) {
        this.roles = roles;
        this.mContent = context;
        mIndicators = new SparseArray<>();
    }

    public void setOnGroupExpandedListener(OnGroupExpandedListener onGroupExpandedListener) {
        mOnGroupExpandedListener = onGroupExpandedListener;
    }

    //            根据分组的展开闭合状态设置指示器
    public void setIndicatorState(int groupPosition, boolean isExpanded) {
        if (isExpanded) {
            mIndicators.get(groupPosition).setImageResource(R.drawable.ic_expand_less);
        } else {
            mIndicators.get(groupPosition).setImageResource(R.drawable.ic_expand_more);
        }
    }

    @Override
    public int getGroupCount() {
        return (null != roles) ? roles.size() : 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
//        return childData[groupPosition].length;
        return roles.get(groupPosition).getUsers().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
//        return groupData[groupPosition];
        return roles.get(groupPosition).getUsers();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
//        return childData[groupPosition][childPosition];
        return roles.get(groupPosition).getUsers().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand_group_indicator, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.label_group_indicator);
            groupViewHolder.ivIndicator = (ImageView) convertView.findViewById(R.id.iv_indicator);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
//        groupViewHolder.tvTitle.setText(groupData[groupPosition]);
        groupViewHolder.tvTitle.setText(roles.get(groupPosition).getName());
        //      把位置和图标添加到Map
        mIndicators.put(groupPosition, groupViewHolder.ivIndicator);
        //      根据分组状态设置Indicator
        setIndicatorState(groupPosition, isExpanded);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View
            convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand_child, parent, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.tvTitle = convertView.findViewById(R.id.label_expand_child);
            childViewHolder.icon = convertView.findViewById(R.id.iv_chile_icon);
            childViewHolder.status = convertView.findViewById(R.id.label_expand_child_status);
            childViewHolder.phone = convertView.findViewById(R.id.iv_chile_phone);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }


//        childViewHolder.tvTitle.setText(childData[groupPosition][childPosition]);
        User user = roles.get(groupPosition).getUsers().get(childPosition);
        //  +"----" + user.getAccount()
        childViewHolder.tvTitle.setText(user.getNickname());
        if(user.isOnline()){
            childViewHolder.tvTitle.setTextColor(mContent.getResources().getColor(R.color.user_online_name));
            childViewHolder.status.setText("在线");
            childViewHolder.status.setTextColor(mContent.getResources().getColor(R.color.user_online));
        }else{
            childViewHolder.tvTitle.setTextColor(mContent.getResources().getColor(R.color.user_outline_name));
            childViewHolder.status.setText("离线");
            childViewHolder.status.setTextColor(mContent.getResources().getColor(R.color.user_outline));
        }
        final String phone = user.getPhone();
        childViewHolder.phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(null != listener){
                    listener.phone(phone);
                }
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        Log.d(TAG, "onGroupExpanded() called with: groupPosition = [" + groupPosition + "]");
        if (mOnGroupExpandedListener != null) {
            mOnGroupExpandedListener.onGroupExpanded(groupPosition);
        }
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        Log.d(TAG, "onGroupCollapsed() called with: groupPosition = [" + groupPosition + "]");
    }

    public void setListener(PhoneClickListener listener) {
        this.listener = listener;
    }

    private static class GroupViewHolder {
        TextView tvTitle;
        ImageView ivIndicator;
    }

    private static class ChildViewHolder {
        TextView tvTitle;
        TextView status;//在
        ImageView icon;
        ImageView phone;
    }

    public interface PhoneClickListener{
        void phone(String phone);
    }
}
